﻿using Sunny.UI;
using System;
using System.IO.Pipes;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using SqlSugar.Extensions;
using CoreShop.Services;
using CoreShop.Library;
using CoreShop.Model;

namespace CoreShop.App
{
    public partial class fpgOpraMonitor : UIPage
    {
        public fpgOpraMonitor()
        {
            InitializeComponent();
            //AutoStartHelper startHelper = new AutoStartHelper();            
            //this.autuRun.Checked = startHelper.CheckAutoStar();
            //https://codeleading.com/article/65693940515/
        }

        private void Downfile_setBarprogress(long size)
        {
            this.uiProcessBar1.Invoke(new Action(() =>
            {
                if (size <= this.uiProcessBar1.Maximum)
                {
                    this.uiProcessBar1.Value = (int)size;
                    decimal cur = (decimal)this.uiProcessBar1.Value;
                    decimal max = (decimal)this.uiProcessBar1.Maximum;
                    this.lbl_msg.Text = "当前下载进度:" + (cur / max).ToString("p");
                }
                else
                {
                    this.lbl_msg.Text = "当前下载进度:100%";
                    this.uiProcessBar1.Value = this.uiProcessBar1.Maximum;
                }
            }));
        }

        private void Downfile_initalProgressbar(long size)
        {
            this.uiProcessBar1.Invoke(new Action(() =>
            {
                this.uiProcessBar1.Maximum = (int)size + 1024;
                this.uiProcessBar1.Value = 0;
            }));
        }

        private void setDownFileFlag(bool CheckVersion)
        {
            try
            {
                //if (AppConstVars.DownFileFlag == false)
                //{
                //    AppConstVars.DownFileFlag = true;
                //    DownFile downfile = new DownFile();
                //    downfile.CheckVersion = CheckVersion;
                //    downfile.setBarprogress += Downfile_setBarprogress;
                //    downfile.initalProgressbar += Downfile_initalProgressbar;
                //    downfile.SetLog += DataExc_SetLog;
                //    Thread Syn_thread = new Thread(downfile.DownLoad);
                //    Syn_thread.Start();
                //}
            }
            catch { }
        }

        public void DataExc_SetLog(string msg)
        {
            this.rtxDataLog.Invoke(new Action(() => {
                this.rtxDataLog.AppendText(msg);
                this.rtxDataLog.HideSelection = false;
            }));
        }

        private void btnDownUpdate_Click(object sender, EventArgs e)
        {
            setDownFileFlag(false);
        }

        private void btnSignalClient_Click(object sender, EventArgs e)
        {

        }
        private void fpgOpraMonitor_ReceiveParams(object sender, UIPageParamsArgs e)
        {
            lblUserName.Text = AppConstVars.sysUser.userName;
            lblStoreName.Text = AppConstVars.sysUser.storeName;
            lblStoreId.Text = AppConstVars.sysUser.storeId.ToString();
            channel.Text = AppConstVars.sysUser.channel.ToString();
            adminApiUrl.Text = AppConstVars.systemsetup.adminApiUrl;
            downBaseTimes.Text = AppConstVars.systemsetup.downBaseTimes.ObjToString()+"秒";
            uploadTimes.Text = AppConstVars.systemsetup.uploadTimes.ObjToString() + "秒";
            sVersion.Text = AppConstVars.sPosVersion.ToString()+ AppConstVars.systemsetup.posVersion.ToString();
            var storeModel = new corecmsstoreServices().GetById(AppConstVars.sysUser.storeId);
            if ((storeModel != null) && (storeModel.syncFlag) && !string.IsNullOrEmpty(storeModel.syncUrl)) syncUrl.Text = storeModel.syncUrl.ToString();
        }

        private void autuRun_Click(object sender, EventArgs e)
        {
            //AutoStartHelper startHelper = new AutoStartHelper();
            //startHelper.SetAutoStarShortCut(autuRun.Checked);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {            
            //if (!ShowAskDialog("确认要退出吗？")) return;
            //AppConstVars.ExitFlag = true;
            this.Close();
        }

        private void rtxDataLog_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (rtxDataLog.Text == "cmd")
            {
                btnExeSql.Visible = true;
                rtxDataLog.Text = "";
            }
        }

        public void ExecuteCommand(string msql)
        {
            try
            {
                var db = new sysuserServices().GetInstance();
                db.Ado.ExecuteCommand(msql);
                DataExc_SetLog("执行完成。");
            }
            catch (Exception ex) 
            {
                DataExc_SetLog(ex.Message);
            }
        }

        private void btnExeSql_Click(object sender, EventArgs e)
        {
            ExecuteCommand(rtxDataLog.Text);
        }
    }
}